import common.commonFunction;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.ListEvent;
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.validators.Validator;

[Embed(source="images/error.png")]private var errorIcon:Class;
[Embed(source="images/success.png")]private var successIcon:Class;
[Embed(source="images/reset.png")]private var resetIcon:Class;


public var parentFunction:Function;
public var switchType:String;
public var switchRule:String;
public var oldSemester:String;
public var newProgram:String;
public var newBranch:String;
public var newSpecialization:String;
public var newSemester:String;
public var selectedValues:ArrayCollection;
public var infoObject:Object={};
public var buttonFunction:Function;

/** this method initialize screen elements**/
public function initFormElements():void
{	
	
	newBranchCombo.selectedItem=newBranch;
	newProgramCombo.selectedItem=newProgram;
	newSpecializationCombo.selectedItem=newSpecialization;
	newSemesterCombo.selectedItem=newSemester;
	urlPrefix=resourceManager.getString('Constants','url')+"/programswitch/";
	urlPrefix1=resourceManager.getString('Constants','url')+"/switchRule/";
	urlPrefix2=resourceManager.getString('Constants','url')+"/programCourseType/";
	
	
	infoObject["userId"]=new Date;
	
	var infoSemester:Object={};

	
	infoSemester["userId"] = new Date;
	infoSemester["programId"] = selectedValues.getItemAt(0).programId;
	infoSemester["branchId"] = selectedValues.getItemAt(0).branchId;
	infoSemester["specializationId"] = selectedValues.getItemAt(0).specializationId; 
	
	getSwitchType.send(infoObject);
	getSwitchRule.send(infoObject);
	getProgramDetails.send(infoObject);
	
	getOldSemesters.send(infoSemester);

}

/**
 * This method is for getting the details
 * requered when the UI is loaded
 **/
[Bindable]public var urlPrefix:String;
[Bindable]public var urlPrefix1:String;
[Bindable]public var urlPrefix2:String;
public function onIntialize():void{
	
	urlPrefix=resourceManager.getString('Constants','url')+"/programswitch/";
	urlPrefix1=resourceManager.getString('Constants','url')+"/switchRule/";
	urlPrefix2=resourceManager.getString('Constants','url')+"/programCourseType/";
	
	
	infoObject["userId"]=new Date;
	

	
	getSwitchType.send(infoObject);
	getSwitchRule.send(infoObject);
	getProgramDetails.send(infoObject);
	
	
}

/**
 * Method for getting the list Switch types 
 * defined for the concerned university
 **/ 
 public var switchTypeXml:XML;
 public var switchTypeList:ArrayCollection;
 public function onSwitchTypeSuccess(event:ResultEvent):void{
 	
 	switchTypeXml=event.result as XML;
 	
 	switchTypeList=new ArrayCollection();
 	
 	for each (var o:Object in switchTypeXml.role){
			
			switchTypeList.addItem({id:o.id,description:o.description});
			
		} 	
 	
 	switchTypeCombo.dataProvider=switchTypeXml.role.description;
 	switchTypeCombo.selectedItem=switchType;
 	
 }
 
/**
 * Method retrives the switch rules 
 * defined for the university
 **/  
[Bindable]
public var details: XML;
public var detailslist:ArrayCollection;
public function onSuccess(event:ResultEvent):void{
	
	details=event.result as XML;
	
	
	detailslist =new ArrayCollection();
	
	for each (var o:Object in details.Details){
		
		detailslist.addItem({select:false,ruleId:o.ruleId,ruleCodeOne:o.ruleCodeOne,ruleCodeTwo:o.ruleCodeTwo,
			ruleCodeThree:o.ruleCodeThree,ruleCodeFour:o.ruleCodeFour,ruleCodeFive:o.ruleCodeFive,
			ruleCodeSix:o.ruleCodeSix,ruleFormula:o.ruleFormula});
		
	}		
		switchRuleCombo.dataProvider = details.Details.ruleId;
		switchRuleCombo.selectedItem=switchRule;
		
}
/**
 * Method retrieves the list of programs
 * for the concerned university
 **/ 
[Bindable]
public var detailsXml: XML;
public function onProgramSuccess(event:ResultEvent):void{
	
	detailsXml=event.result as XML;	
	
	var detailslist:ArrayCollection =new ArrayCollection();
	
	for each (var o:Object in detailsXml.role){
		
		detailslist.addItem({id:o.id,description:o.description});
		
	}		
		newProgramCombo.dataProvider = detailsXml.role.description;		
}

public function programChange(event:ListEvent):void
			{
			 newBranchCombo.enabled=true;
			 
			 var infoObject:Object = {};
			 
			 infoObject["userId"] = new Date;
			 infoObject["programId"] = detailsXml.role.(description==newProgramCombo.selectedLabel).id;
			 
			 getBranchDetails.send(infoObject);
			 
			}
/**
 * Method retrieves the list of branches for the selected program
 * of the concerned university
 **/
[Bindable]
public var branchDetails: XML;
public function programChangeSuccess(event:ResultEvent):void{
	
	branchDetails=event.result as XML;
	
	var detailslist:ArrayCollection =new ArrayCollection();
	
	for each (var o:Object in branchDetails.role){
		
		detailslist.addItem({id:o.id,description:o.description});
		
	}		
		newBranchCombo.dataProvider = branchDetails.role.description;		
	
}

protected function branchChange(event:ListEvent):void
			{
	newSpecializationCombo.enabled=true;
	
	var infoObject:Object = {};
	
	infoObject["userId"] = new Date;
	infoObject["programId"] = detailsXml.role.(description==newProgramCombo.selectedLabel).id;
	infoObject["branchId"] = branchDetails.role.(description==newBranchCombo.selectedLabel).id;
	
	getspecializationDetails.send(infoObject);	
			}
/**
 * Method retrieves the list of specializations defined
 * for the selected program-branch
 * of the concerned university
 **/
[Bindable]
public var specialiationDetails: XML;
public function branchChangeSuccess(event:ResultEvent):void{
	
	specialiationDetails=event.result as XML;
	
	var detailslist:ArrayCollection =new ArrayCollection();
	
	for each (var o:Object in specialiationDetails.role){
		
		detailslist.addItem({id:o.id,description:o.description});
		
	}		
		newSpecializationCombo.dataProvider = specialiationDetails.role.description;	
}
			
protected function specializationChange(event:ListEvent):void
			{
				newSemesterCombo.enabled=true;
				var infoObject:Object = {};
				
				infoObject["userId"] = new Date;
				infoObject["programId"] = detailsXml.role.(description==newProgramCombo.selectedLabel).id;
				infoObject["branchId"] = branchDetails.role.(description==newBranchCombo.selectedLabel).id;
				infoObject["specializationId"] =  specialiationDetails.role.(description==newSpecializationCombo.selectedLabel).id;
				
				getsemesterDetails.send(infoObject);				
			}	

/**
 * Method retrieves the list of semestes defined
 * for the selected program-branch-specialization
 * of the concerned university
 **/			
[Bindable]
public var semesterDetails: XML;
public function specializationChangeSuccess(event:ResultEvent):void{
	
	semesterDetails=event.result as XML;	
	
	var detailslist:ArrayCollection =new ArrayCollection();
	
	for each (var o:Object in semesterDetails.role){
		
		detailslist.addItem({id:o.id,description:o.description});
		
	}	
		newSemesterCombo.dataProvider = semesterDetails.role.description;
	
}
  /**
 	* Mehtod to be called on request failure
 **/
 public function onFailure(event:FaultEvent):void{
 	
 	Alert.show(commonFunction.getMessages('failure'),
 	commonFunction.getMessages('error'),4,null,null,errorIcon);
 	
 }

/** this method close popup screen**/
public function closeScreen():void
{
	PopUpManager.removePopUp(this);
	
	
}

/** this method add program switch details **/
public function submitDetails():void
{
	if(validateAddProgramSwitchScreen())
	{
		
		infoObject["swtichType"]=switchTypeXml.role.(description==switchTypeCombo.selectedLabel).id;
		infoObject["switchRuleId"]=switchRuleCombo.selectedLabel;
		infoObject["oldProgramId"]=selectedValues[0].programId;
		infoObject["oldBranchId"]=selectedValues[0].branchId;
		infoObject["oldSpecializationId"]=selectedValues[0].specializationId;
		infoObject["oldSemesterId"]=oldSemesterDetails.role.(description==oldSemesterCombo.selectedLabel).id;
		infoObject["newProgramId"] = detailsXml.role.(description==newProgramCombo.selectedLabel).id;
		infoObject["newBranchId"] = branchDetails.role.(description==newBranchCombo.selectedLabel).id;
		infoObject["newSpecializationId"] =  specialiationDetails.role.(description==newSpecializationCombo.selectedLabel).id;
		infoObject["newSemesterId"] =  semesterDetails.role.(description==newSemesterCombo.selectedLabel).id;
		infoObject["activity"]="insert";		
		
		setProgramSwitchDetails.send(infoObject);		
	}
	else
	{
		Alert.show(commonFunction.getMessages('error'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	}
}

public var oldSemesterDetails:XML;
public function onOldSemester(event:ResultEvent):void{
	
	oldSemesterDetails = event.result as XML;
	
	
	oldSemesterCombo.dataProvider = oldSemesterDetails.role.description;
	
}

/**
 * Method to be executed on successful activity(insert/update) execution
 **/ 
public var successDetails:XML;
public function onSuccessfulEntry(event:ResultEvent):void{
	
	successDetails=event.result as XML;
	
	if(successDetails.Details.list_values=="success"){
	
	Alert.show((resourceManager.getString('Messages','savedSuccessfully')),
		(resourceManager.getString('Messages','saved')),
		4,null,null,successIcon);
		
	}else{
		Alert.show(commonFunction.getMessages('failure'),
 	commonFunction.getMessages('error'),4,null,null,errorIcon);
	}
		
		closeScreen();	
		
		buttonFunction();	
}

/** this method update program switch details**/
public function updateDetails():void
{
	if(validateAddProgramSwitchScreen())
	{
		infoObject["swtichType"]= selectedValues[0].componentId;
		infoObject["oldSwitchRuleId"]=selectedValues[0].switchRuleId;
		infoObject["switchRuleId"]= switchRuleCombo.selectedLabel;
		infoObject["oldProgramId"]= selectedValues[0].oldProgramId;
		infoObject["oldBranchId"]= selectedValues[0].oldBranchId;
		infoObject["oldSpecializationId"]= selectedValues[0].oldSpecializationId;
		infoObject["oldSemesterId"]= oldSemesterDetails.role.(description==oldSemesterCombo.selectedLabel).id;
		infoObject["newProgramId"] = detailsXml.role.(description==newProgramCombo.selectedLabel).id;
		infoObject["newBranchId"] = branchDetails.role.(description==newBranchCombo.selectedLabel).id;
		infoObject["newSpecializationId"] =  specialiationDetails.role.(description==newSpecializationCombo.selectedLabel).id;
		infoObject["newSemesterId"] =  semesterDetails.role.(description==newSemesterCombo.selectedLabel).id;
		infoObject["activity"]="update";		
		
		setProgramSwitchDetails.send(infoObject);
	}
	else
	{
		Alert.show(commonFunction.getMessages('failure'),
 	commonFunction.getMessages('error'),4,null,null,errorIcon);
	}
}

/** this method reset program switch screen **/
public function resetDetails():void
{
	switchTypeCombo.selectedIndex=-1
	switchRuleCombo.selectedIndex=-1
	oldSemesterCombo.selectedIndex=-1
	newProgramCombo.selectedIndex=-1
	newBranchCombo.selectedIndex=-1
	newSpecializationCombo.selectedIndex=-1
	newSemesterCombo.selectedIndex=-1

	switchTypeCombo.errorString="";
	switchRuleCombo.errorString="";
	oldSemesterCombo.errorString="";
	newProgramCombo.errorString="";
	newBranchCombo.errorString="";
	newSpecializationCombo.errorString="";
	newSemesterCombo.errorString="";
	
}

/** this method validate program switch screen **/
public function validateAddProgramSwitchScreen():Boolean
{
	switchTypeValidator.source=switchTypeCombo
	switchRuleValidator.source=switchRuleCombo;
	newProgramValidator.source=newProgramCombo;
	newBranchValidator.source=newBranchCombo;
	newSpecializationValidator.source=newSpecializationCombo;
	oldSemesterValidator.source=oldSemesterCombo;
	newSemesterValidator.source=newSemesterCombo;
	
	if(Validator.validateAll([switchRuleValidator, switchTypeValidator, newProgramValidator, newBranchValidator,
	                          newSpecializationValidator, newSemesterValidator,oldSemesterValidator]).length==0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
